package org.charry.lib.commons.math;

import java.math.BigDecimal;

import org.charry.lib.commons.lang.Z;


/**
 * Common mathematical operations.
 * 
 * @author wcharry
 * 
 */
public final class MathUtility {
	/**
	 * Dummy constructor.
	 */
	private MathUtility() {
		// NOOP
	}

	/**
	 * A function to format numbers.
	 * 
	 * @param value
	 *            value to be formed
	 * @param scale
	 *            scale
	 * @param roundingMode
	 *            rounding mode
	 * @return result
	 */
	public static double round(final double value, final int scale,
			final int roundingMode) {
		BigDecimal bd = new BigDecimal(value);
		bd = bd.setScale(scale, roundingMode);
		double d = bd.doubleValue();
		bd = null;

		return d;
	}

	/**
	 * A function to change number to percentage.
	 * 
	 * @param value
	 *            value
	 * @param scale
	 *            scale
	 * @param roundingMode
	 *            roudingMode
	 * @return percentage
	 */
	public static String toPercent(final double value, final int scale,
			final int roundingMode) {
		String p = "";
		BigDecimal bd = new BigDecimal(value);
		bd = bd.multiply(new BigDecimal(100));
		bd = bd.setScale(scale, roundingMode);
		double val = bd.doubleValue();
		bd = null;
		p = val + "%";

		return p;
	}

	public static String toPercent(final double value) {
		return toPercent(value, 2, BigDecimal.ROUND_HALF_DOWN);
	}

	public static String toPercent(final double value, final int scale) {
		return toPercent(value, scale, BigDecimal.ROUND_HALF_DOWN);
	}
}
